package com.hashbee.bbs.controller;

import com.hashbee.bbs.bo.IsLike;
import com.hashbee.bbs.bo.PageData;
import com.hashbee.bbs.bo.PagePosts;
import com.hashbee.bbs.bo.para.PageQueryPara;
import com.hashbee.bbs.bo.para.PostPara;
import com.hashbee.bbs.domain.Posts;
import com.hashbee.bbs.service.PostService;
import com.hashbee.bo.ReturnData;
import com.hashbee.user.model.UserVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/bbs/post")
public class PostController {

    private static final Logger logger = LoggerFactory.getLogger(PostController.class);

    @Autowired
    private PostService postService;

    @PostMapping("/create")
    public ReturnData create(@RequestParam(value = "forumId", required = true) String forumId,
                             @RequestParam(value = "subject", required = true) String subject,
                             @RequestParam(value = "content", required = true) String content,
                             @RequestParam(value = "parentId", required = false) String parentId,
                             @RequestParam(value = "categoryId", required = false, defaultValue = "0") Integer categoryId,
                             @RequestParam(value = "cover", required = false, defaultValue = "") String cover,
                             @RequestParam(value = "tags", required = false, defaultValue = "") String tags) {
        // 返回对象
        ReturnData<String> returnData = new ReturnData<>();

        PostPara postPara = new PostPara();
        postPara.setForumId(forumId);
        postPara.setSubject(subject);
        postPara.setContent(content);
        postPara.setParentId(parentId);
        postPara.setCategoryId(categoryId);
        postPara.setCover(cover);
        postPara.setTags(tags);

        returnData = postService.create(postPara);

        return returnData;
    }

    /**
     * 帖子分页查询
     * @param forumId 论坛ID
     * @param pageIndex 页码
     * @param pageSize 每页数量
     * @return
     */
    @GetMapping("/topic/page")
    public ReturnData<PageData<Posts>> page(@RequestParam(value = "forumId", required = false) String forumId,
                           @RequestParam(value = "pageIndex", required = false, defaultValue = "0") int pageIndex,
                           @RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize) {

        UserVo user = null;
        try {
            user = (UserVo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        } catch (Exception e) {

        }
        // 返回对象
        ReturnData<PageData<Posts>> returnData = new ReturnData<>();

        // 参数对象
        PageQueryPara pageQueryPara = new PageQueryPara();
        pageQueryPara.setForumId(forumId);
        pageQueryPara.setPageIndex(pageIndex);
        pageQueryPara.setPageSize(pageSize);

        // 调用服务获取数据
        PageData<Posts> postsPageData = postService.queryTopic(pageQueryPara);

        // 设置数据
        returnData.setData(postsPageData);

        return returnData;
    }

    @GetMapping("/detail")
    public ReturnData detail(@RequestParam(value = "postId", required = true) String postId) {
        // 返回对象
        ReturnData<Posts> returnData = new ReturnData<>();

        // 获取数据
        Posts posts = postService.queryDetail(postId);
        returnData.setData(posts);

        // 设置数据

        return returnData;
    }

    @GetMapping("/reply")
    public ReturnData reply(@RequestParam(value = "postId", required = true) String postId,
                            @RequestParam(value = "pageIndex", required = false, defaultValue = "0") int pageIndex,
                            @RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize) {
        // 返回对象
        ReturnData<PagePosts> returnData = new ReturnData<>();

        // 参数对象
        PageQueryPara pageQueryPara = new PageQueryPara();
        pageQueryPara.setParentId(postId);
        pageQueryPara.setPageIndex(pageIndex);
        pageQueryPara.setPageSize(pageSize);

        // 调用服务获取数据
        PagePosts pagePosts = postService.queryReply(pageQueryPara);

        // 设置数据
        returnData.setData(pagePosts);

        return returnData;
    }

    @PostMapping("/like")
    public ReturnData like(@RequestParam(value = "postId", required = true) String postId,
                           @RequestParam(value = "isLike", required = true) int isLike) {

        ReturnData returnData = new ReturnData();
        IsLike isLikeParams = new IsLike();
        isLikeParams.setIsLike(isLike);
        isLikeParams.setPostId(postId);
        postService.like(isLikeParams);
        return returnData;
    }
}
